<!--
 * @Author: ZAL 2177624751@qq.com
 * @Date: 2025-03-31 21:30:00
 * @LastEditors: ZAL 2177624751@qq.com
 * @LastEditTime: 2025-03-31 21:26:33
 * @Description: 红包页面
-->
<template>
  <view class="container">
    <!-- 红包卡片 -->
    <view class="redpacket-card">
      <view class="redpacket-header">
        <text class="title">我的红包</text>
        <text class="amount">￥{{ totalAmount }}</text>
      </view>
      <view class="redpacket-footer">
        <view class="footer-item">
          <text class="label">可使用</text>
          <text class="value">{{ availableCount }}个</text>
        </view>
        <view class="footer-item">
          <text class="label">已使用</text>
          <text class="value">{{ usedCount }}个</text>
        </view>
      </view>
    </view>

    <!-- 红包列表 -->
    <view class="redpacket-list">
      <view class="section-title">红包列表</view>
      <scroll-view 
        class="list-content" 
        scroll-y 
        @scrolltolower="loadMore"
        v-if="redpacketList.length > 0"
      >
        <view 
          class="redpacket-item" 
          v-for="item in redpacketList" 
          :key="item.id"
          :class="{ 'expired': item.status === 'expired' }"
        >
          <view class="item-left">
            <view class="amount-wrap">
              <text class="currency">￥</text>
              <text class="amount">{{ item.amount }}</text>
            </view>
            <text class="condition">满{{ item.minAmount }}元可用</text>
          </view>
          <view class="item-right">
            <view class="info-wrap">
              <text class="title">{{ item.title }}</text>
              <text class="time">{{ item.validTime }}</text>
            </view>
            <text class="status" :class="{ 'status-used': item.status === 'used' }">
              {{ item.statusText }}
            </text>
          </view>
        </view>
      </scroll-view>
      <view v-else class="empty-state">
        <image src="/static/images/empty-redpacket.png" mode="aspectFit" class="empty-image" />
        <text>暂无红包</text>
      </view>
    </view>

    <!-- 使用规则 -->
    <view class="redpacket-rules">
      <view class="section-title">使用规则</view>
      <view class="rules-content">
        <view class="rule-item" v-for="(rule, index) in rules" :key="index">
          <text class="rule-title">{{ rule.title }}</text>
          <text class="rule-desc">{{ rule.desc }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { usePointsStore } from '/store/pointsStore'

const pointsStore = usePointsStore()

// 积分转换为金额 (100积分 = 1元)
const convertPointsToAmount = (points) => {
  return (points / 100).toFixed(2)
}

// 计算总金额
const totalAmount = computed(() => {
  return convertPointsToAmount(pointsStore.getPointsOverview.totalPoints || 0)
})

// 计算可用和已用数量
const availableCount = computed(() => {
  return pointsStore.getPointsRecords.filter(item => item.status === 0).length
})

const usedCount = computed(() => {
  return pointsStore.getPointsRecords.filter(item => item.status === 1).length
})

// 转换积分记录为红包列表
const redpacketList = computed(() => {
  return pointsStore.getPointsRecords.map(item => ({
    id: item.id,
    amount: convertPointsToAmount(item.points),
    minAmount: convertPointsToAmount(item.points * 2), // 假设使用条件为积分的2倍
    title: item.typeDesc,
    validTime: item.expireTime || item.createTime,
    status: item.status === 0 ? 'available' : 'used',
    statusText: item.status === 0 ? '可使用' : '已使用'
  }))
})

const rules = ref([
  {
    title: '使用条件',
    desc: '每个订单仅可使用一个红包，100积分可抵扣1元'
  },
  {
    title: '有效期',
    desc: '红包有效期以实际显示时间为准'
  },
  {
    title: '使用范围',
    desc: '红包可用于抵扣商品实际支付金额'
  }
])

// 初始化数据
onMounted(async () => {
  try {
    await pointsStore.loadPointsOverview()
    await pointsStore.loadPointsRecords()
  } catch (error) {
    uni.showToast({
      title: '加载数据失败',
      icon: 'none'
    })
  }
})

// 加载更多
const loadMore = async () => {
  try {
    await pointsStore.loadPointsRecords()
  } catch (error) {
    uni.showToast({
      title: '加载更多失败',
      icon: 'none'
    })
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-bottom: env(safe-area-inset-bottom);
}

.redpacket-card {
  margin: 20rpx;
  padding: 40rpx;
  background: linear-gradient(135deg, #00b578, #07c160);
  border-radius: 20rpx;
  color: #fff;

  .redpacket-header {
    margin-bottom: 40rpx;

    .title {
      font-size: 28rpx;
      opacity: 0.9;
    }

    .amount {
      display: block;
      font-size: 60rpx;
      font-weight: bold;
      margin-top: 20rpx;
    }
  }

  .redpacket-footer {
    display: flex;
    justify-content: space-around;
    border-top: 1rpx solid rgba(255, 255, 255, 0.2);
    padding-top: 30rpx;

    .footer-item {
      text-align: center;

      .label {
        font-size: 24rpx;
        opacity: 0.9;
      }

      .value {
        display: block;
        font-size: 32rpx;
        font-weight: bold;
        margin-top: 10rpx;
      }
    }
  }
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin: 30rpx 20rpx 20rpx;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 6rpx;
    height: 30rpx;
    background-color: #00b578;
    border-radius: 3rpx;
  }
}

.redpacket-list {
  background-color: #fff;
  margin: 20rpx;
  border-radius: 12rpx;
  padding: 20rpx;

  .list-content {
    height: 400rpx;
  }

  .redpacket-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    margin-bottom: 20rpx;
    background: linear-gradient(135deg, #f0fff4, #fff);
    border-radius: 12rpx;
    border: 1rpx solid #b7eb8f;

    &.expired {
      opacity: 0.5;
      background: #f5f5f5;
      border-color: #ddd;
    }

    .item-left {
      .amount-wrap {
        display: flex;
        align-items: baseline;

        .currency {
          font-size: 32rpx;
          color: #00b578;
          font-weight: bold;
        }

        .amount {
          font-size: 48rpx;
          color: #00b578;
          font-weight: bold;
          margin-left: 4rpx;
        }
      }

      .condition {
        font-size: 24rpx;
        color: #999;
        margin-top: 10rpx;
      }
    }

    .item-right {
      text-align: right;

      .info-wrap {
        margin-bottom: 10rpx;

        .title {
          font-size: 28rpx;
          color: #333;
          margin-bottom: 6rpx;
          display: block;
        }

        .time {
          font-size: 24rpx;
          color: #999;
        }
      }

      .status {
        font-size: 24rpx;
        color: #52c41a;
        
        &.status-used {
          color: #999;
        }
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx 0;

  .empty-image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }

  text {
    font-size: 28rpx;
    color: #999;
  }
}

.redpacket-rules {
  background-color: #fff;
  margin: 20rpx;
  border-radius: 12rpx;
  padding: 20rpx;

  .rules-content {
    .rule-item {
      margin-bottom: 30rpx;

      .rule-title {
        font-size: 28rpx;
        color: #333;
        font-weight: bold;
        margin-bottom: 10rpx;
        display: block;
      }

      .rule-desc {
        font-size: 24rpx;
        color: #666;
        line-height: 1.6;
      }
    }
  }
}
</style> 